package online.xybh.qa.controller;

import com.alibaba.fastjson.JSONObject;
import online.xybh.qa.dto.CommonQuestion;
import online.xybh.qa.dto.TeacherAndClassDTO;
import online.xybh.qa.service.CommonQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author: xybh
 * @Description:
 * @Date: Created in 22:45 2020/10/23
 * @Modified:
 */
@Controller
public class CommonQuestionController {

    @Autowired
    private CommonQuestionService commonQuestionService;

    @PostMapping(value = "createCommonQuestion", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> createCommonQuestion(@RequestBody JSONObject question,
                                                    HttpSession session) {
        TeacherAndClassDTO user = (TeacherAndClassDTO) session.getAttribute("user");
        String title = question.getString("title");
        Integer type = question.getInteger("type");
        String answer = question.getString("answer");
        String parse = question.getString("parse");
        String[] options = question.getObject("options", String[].class);
        System.out.println(Arrays.toString(options));
        commonQuestionService.createCommonQuestion(title, type, answer, parse, options, user.getSubject(), user.getTeacherId());
        HashMap<String, Object> res = new HashMap<>();
        res.put("code", 200);
        return res;
    }

    @RequestMapping("commonQuestion/{id}")
    public String queryCommonQuestionById(@PathVariable("id") String id,
                                          HttpSession session,
                                          Model model) {
        CommonQuestion question = commonQuestionService.queryCommonQuestionById(id);
        TeacherAndClassDTO user = (TeacherAndClassDTO) session.getAttribute("user");
        model.addAttribute("user", user);
        model.addAttribute("question", question);
        model.addAttribute("backUrl", "/commonQuestion/"+id);
        return "commonquestion_info";
    }

    @RequestMapping("commonQuestion_JSON/{id}")
    @ResponseBody
    public CommonQuestion queryCommonQuestionById_JSON(@PathVariable("id") String id,
                                          HttpSession session,
                                          Model model) {
        CommonQuestion question = commonQuestionService.queryCommonQuestionById(id);
        return question;
    }

    @RequestMapping("deleteCommonQuestion/{id}")
    public String deleteCommonQuestionById(@PathVariable("id") String id,
                                           HttpSession session,
                                           Model model){
        commonQuestionService.deleteCommonQuestionById(id);
        TeacherAndClassDTO user = (TeacherAndClassDTO) session.getAttribute("user");
        model.addAttribute("user", user);
        model.addAttribute("backUrl", "/commonQuestion/"+id);
        return "redirect:/FAQ";
    }
}
